var util = require('util.js')

function ApiGet(url, data, cb) {
  wx.showNavigationBarLoading();
  var token = wx.getStorageSync("token");
  data["token"] = token;

  wx.request({
    method: 'GET',
    url: url,
    data: data,
    header: {
      "Content-Type": "application/json"
    },
    success: (res) => {
      // console.log("Get:"+res);
      var json = res.data;
      if (typeof json != 'object') {
        json = json.replace("\ufeff", "");
        var jj = JSON.parse(json);
        res.data = jj;
      }

      typeof cb == "function" && cb(res.data, "");
      wx.hideNavigationBarLoading();
    },
    fail: (err) => {
      console.log("err:"+err);
      typeof cb == "function" && cb(null, err.errMsg);
      wx.hideNavigationBarLoading();
    }
  });
};

function ApiPost(url, data, cb) {
  var token = wx.getStorageSync("token");
  data["token"] = token;

  wx.request({
    method: 'POST',
    url: url,
    data: data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: (res) => {
      var json = res.data;
      if (typeof json != 'object') {
        json = json.replace("\ufeff", "");
        var jj = JSON.parse(json);
        res.data = jj;
      }

      typeof cb == "function" && cb(res.data, "");
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
      console.log("http请求:" + url);
      console.log(err)
    }
  });
};

function Get(url, data, cb) {
  wx.showNavigationBarLoading();
  wx.request({
    method: 'GET',
    url: url,
    data: data,
    header: {
      "Content-Type": "application/json"
    },
    success: (res) => {
      // console.log("Get:"+res);
      var json = res.data;
      if (typeof json != 'object') {
        json = json.replace("\ufeff", "");
        var jj = JSON.parse(json);
        res.data = jj;
      }

      typeof cb == "function" && cb(res.data, "");
      wx.hideNavigationBarLoading();
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
      wx.hideNavigationBarLoading();
    }
  });
};

function Post(url, data, cb) {
  wx.request({
    method: 'POST',
    url: url,
    data: data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: (res) => {
      var json = res.data;
      if (typeof json != 'object') {
        json = json.replace("\ufeff", "");
        var jj = JSON.parse(json);
        res.data = jj;
      }

      typeof cb == "function" && cb(res.data, "");
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
      console.log("http请求:" + url);
      console.log(err)
    }
  });
};

function ApiUpload(url, file, data, cb) {
  wx.uploadFile({
    url: url,
    filePath: file,
    name: "file",
    formData: data,
    success: (res) => {
      if (typeof(res.data) == "string") {
        typeof cb == "function" && cb(JSON.parse(res.data), "");
      } else {
        typeof cb == "function" && cb(res.data, "");
      }
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
    }
  });
};

function Upload(url, file, data, cb) {
  wx.uploadFile({
    url: url,
    filePath: file,
    name: "file",
    formData: data,
    success: (res) => {
      if (typeof(res.data) == "string") {
        typeof cb == "function" && cb(JSON.parse(res.data), "");
      } else {
        typeof cb == "function" && cb(res.data, "");
      }

    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
    }
  });
};


module.exports = {
  httpGet: Get,
  httpPost: Post,
  httpUpload: Upload,
  ApiGet: ApiGet,
  ApiPost: ApiPost,
  ApiUpload: ApiUpload
};